package com.haibei.adapter;

import java.util.ArrayList;
import java.util.HashSet;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.cache.SimpleImgLoaderLru;
import com.haibei.R;
import com.haibei.adapter.bean.CollectArticle;
import com.haibei.adapter.bean.CollectGoods;

public class CollectGoodsAdapter extends BaseAdapter {

	private ArrayList<CollectGoods> mArrayListDataList;
	private Context mContext;
	private boolean isEdit = false;
	private HashSet<CollectGoods> mHashSetSelect;

	public CollectGoodsAdapter(Context context, ArrayList<CollectGoods> list) {
		this.mContext = context;
		this.mArrayListDataList = list;
		this.mHashSetSelect = new HashSet<CollectGoods>();
	}

	public void removeSelect() {
		ArrayList<CollectGoods> needDletes = new ArrayList<CollectGoods>();
		for (CollectGoods artice : mArrayListDataList) {
			if (mHashSetSelect.contains(artice)) {
				needDletes.add(artice);
			}
		}
		mArrayListDataList.removeAll(needDletes);
		mHashSetSelect.clear();
		notifyDataSetChanged();
	}

	public HashSet<CollectGoods> getSelectSet() {
		return mHashSetSelect;
	}

	@Override
	public int getCount() {
		return mArrayListDataList.size();
	}

	@Override
	public Object getItem(int position) {
		return mArrayListDataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vl = null;
		if (null == convertView) {
			vl = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.item_collect_goods, null);
			vl.imageView = (ImageView) convertView.findViewById(R.id.imageView);
			vl.textViewName = (TextView) convertView.findViewById(R.id.textViewName);
			vl.textViewNowPrice = (TextView) convertView.findViewById(R.id.textViewNowPrice);
			vl.textViewOrginPrice = (TextView) convertView.findViewById(R.id.textViewOrginPrice);
			vl.textViewSeldCount = (TextView) convertView.findViewById(R.id.textViewSeldCount);
			vl.textViewCollect = (TextView) convertView.findViewById(R.id.textViewCollectCount);
			vl.relativeLayoutRadio = convertView.findViewById(R.id.relativeLayoutRadio);
			vl.checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxDel);

			convertView.setTag(vl);
		} else {
			vl = (ViewHolder) convertView.getTag();
		}
		final CollectGoods gods = mArrayListDataList.get(position);
		vl.textViewName.setText(gods.getTitle());
		vl.textViewNowPrice.setText("￥" + gods.getSalePrice());
		if (gods.isPromotion()) {
			vl.textViewOrginPrice.setVisibility(View.VISIBLE);
			vl.textViewOrginPrice.setText("￥" + gods.getMarketPrice());
			vl.textViewOrginPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
			// textview.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
		} else {
			vl.textViewOrginPrice.setVisibility(View.INVISIBLE);
		}
		vl.textViewSeldCount.setText("已售:" + gods.getSales());
		vl.textViewCollect.setText("收藏:" + gods.getFavoriteCount());
		SimpleImgLoaderLru.from(mContext).displayImage(vl.imageView, gods.getThumbnailImg(), R.drawable.bg_default_item_img);
		// ImageManagerLru.from(mContext).displayImage(vl.imageView,
		// gods.getThumbnailImg(), R.drawable.bg_default_item_img);
		if (isEdit) {
			vl.relativeLayoutRadio.setVisibility(View.VISIBLE);
			vl.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						mHashSetSelect.add(gods);
					} else {
						mHashSetSelect.remove(gods);
					}
				}
			});
			if (mHashSetSelect.contains(gods)) {
				vl.checkBox.setChecked(true);
			}
		} else {
			vl.relativeLayoutRadio.setVisibility(View.GONE);
			vl.checkBox.setOnCheckedChangeListener(null);
		}

		return convertView;
	}

	public void setEditEable(boolean isEdit) {
		this.isEdit = isEdit;
		notifyDataSetChanged();
	}

	public boolean isEdit() {
		return isEdit;
	}

	class ViewHolder {
		ImageView imageView;
		TextView textViewName;
		TextView textViewNowPrice;
		TextView textViewOrginPrice;
		TextView textViewSeldCount;
		TextView textViewCollect;
		View relativeLayoutRadio;
		CheckBox checkBox;
	}
}
